package com.yang.controller.common;

import com.yang.utils.AliOssUtil;
import com.yang.vo.ResponseResult;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.UUID;

@Api(tags = "文件上传")
@CrossOrigin
@RestController
@RequestMapping("/file")
public class FileUpLoadController {
    @PostMapping("/upload")
    public ResponseResult upload(MultipartFile file){
        String url;
        try {
            String originName= file.getOriginalFilename();
            String newName=UUID.randomUUID()+originName.substring(originName.lastIndexOf("."));
            url=AliOssUtil.upload(file.getBytes(), newName);
        } catch (IOException e) {
            return ResponseResult.fail("上传失败!");
        }
        return ResponseResult.success("上传成功!",url);
    }
}